dojo.provide('clutch.form');

dojo.declare(
    'clutch.form.Form',
    null,
    {
        fields:null,
        constructor: function(form_id) {
            console.log('Constructing form', form_id);
            this.dom = dojo.byId(form_id);
            this.fields = [];
            dojo.forEach(dojo.query('input', this.dom), function(node) {
                var widget = dijit.getEnclosingWidget(node);
                this.fields.push({widget:widget, dom:node});
            }, this);
            dojo.forEach(dojo.query('select', this.dom), function(node) {
                var widget = dijit.getEnclosingWidget(node);
                this.fields.push({widget:widget, dom:node});
            }, this);
            console.log('Form constructed');
        },
        setValue: function(value) {
            console.log('Setting form value to', value);
            dojo.forEach(this.fields, function(field) {
                console.log('Examinging field', field.widget.name, field.dom);
                if(value[field.widget.name]) {
                    field.widget.attr('value', value[field.widget.name])
                }
            });
        }
    }
);
